<template>
  <div style="display: flex; justify-content: space-around">
    <el-form ref="ruleForm" :model="createInfo" :rules="rules" label-width="80px" class="demo-ruleForm">
      <el-form-item label="名称" prop="pvcName" style="margin-bottom:30px">
        <el-input v-model="createInfo.pvcName" placeholder="请填写持久卷声明名称" clearable style="width: 350px;"/>
      </el-form-item>
      <el-form-item label="访问模式" prop="pvcAccessModes" style="margin-bottom:30px">
        <el-select v-model="createInfo.pvcAccessModes" placeholder="请填写持久卷声明访问模式" clearable
                   style="width: 350px;"
        >
          <el-option label="ReadWriteOnce" value="ReadWriteOnce"/>
          <el-option label="ReadWriteMany" value="ReadWriteMany"/>
          <el-option label="ReadOnlyMany" value="ReadOnlyMany"/>
        </el-select>
      </el-form-item>
      <el-form-item label="容量" prop="pvcCapacity" style="margin-bottom:30px">
        <el-input
          v-model="createInfo.pvcCapacity"
          style="width: 350px"
          controls-position="right"
          :min="0"
          :max="1024"
          :step="1"
          type="number"
        >
          <template v-slot:append>GB</template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div style="float: right;">
          <el-button type="primary" size="small" @click="createPvc('ruleForm')"
                     class="el-button-primary-style"
          >确定
          </el-button>
          <el-button type="primary" size="small" @click="resetForm('ruleForm')"
                     class="el-button-primary-style"
          >取消
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  export default {
    name: 'CreatePersistentVolumeClaim',
    data() {
      return {
        createInfo: {
          pvcName: '',
          pvcAccessModes: 'ReadWriteOnce',
          pvcCapacity: '1'
        },
        rules: {
          pvcName: [
            {required: true, message: '请填写持久卷声明的名称', trigger: 'blur'},
            {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'},
            {
              pattern: /^[a-z0-9][a-z0-9-]*[a-z0-9]$/,
              message: '名称格式不正确,只能包含小写字母、数字和连字符(-),必须以小写字母或数字开头和结尾',
              trigger: 'blur'
            }
          ],
          pvcAccessModes: [
            {required: true, message: '请填写持久卷声明访问模式', trigger: 'blur'},
            {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
          ],
          pvcCapacity: [
            {required: true, message: '请填写持久卷声明容量值', trigger: 'blur'},
            {
              validator: (rule, value, callback) => {
                if (!Number.isInteger(Number(value))) {
                  callback(new Error('请输入整数'))
                } else {
                  callback()
                }
              },
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      createPvc(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$storagePvcApi.createPvc(this.createInfo).then(res => {
              if (res && res.code === 200) {
                this.$message.success(res.data)
                this.$emit('complete')
              }
            })
          } else {
            this.$message.warning('有数据未填写，请填写')
            return false
          }
        })
      },
      resetForm(formName) {
        this.$emit('closeCreatePersistentVolumeClaimDialog')
      }
    }
  }
</script>

<style lang="css" scoped>
</style>
